<?php
/**
 * phpRPG
 * Copyright 2011 phpRPG, All Rights Reserved
 * @package phpRPG
 * @author Mike V.
 *
 * Website: http://phprpg.net
 **/
require("global.php");

$query = $db->query("SELECT * FROM `".DB_PRE."players`");
$r = $db->recordcount($query);
 
$rowsperpage = 20; // number of results shown on each page, manual for now
$totalpages = ceil($r/$rowsperpage);
 
if(isset($phprpg->input['page']) && is_numeric($phprpg->input['page']))
{
	$currentpage = (int)$phprpg->input['page'];
}
else
{
	$currentpage = 1;
}

if($currentpage > $totalpages) // if current page is higher than total pages, set current to total
{
	$currentpage = $totalpages;
}

$offset = ($currentpage-1) * $rowsperpage;
#($offset>0) ? null : $offset = 0; //ignore this

if($currentpage < 1) // if current page is lower than lowest, set current = 1
{
	$currentpage = 1;
}

$players = $db->select("players","*","",array("limit" => "$offset, $rowsperpage"));
while($player = $db->fetchrow($players))
{
	$mlist .= "<tr>";
	$mlist .= "<td><a href=\"profile.php?pid={$player['pid']}\">{$player['username']}</a></td>";
	$mlist .= "<td>{$player['level']}</td>";
	$mlist .= "<td><a href=\"fight.php?pid={$player['pid']}\">Fight</a></td>";
	$mlist .= "</tr>";
}

// if the current page is higher than page 1, show links for previous pages
if($currentpage > 1)
{
	$prev = intval($currentpage) - 1;
	$prevpages .= " <a href=\"memberlist.php?page=1\">&lt;&lt;</a> "; // link << to go back to page 1
	$prevpages .= " <a href=\"memberlist.php?page=$prev\">&lt;</a> "; // link < to go back 1 page
	if($currentpage > 2)
	{
		$prev2 = $prev - 1;
		$prevpages .= " <a href=\"memberlist.php?page=$prev2\">$prev2</a> ";
	}
	$prevpages .= " <a href=\"memberlist.php?page=$prev\">$prev</a> ";
}

// add current page to template output and bold it
$pages .= " <b><a href=\"memberlist.php?page=$currentpage\">$currentpage</a></b> ";

// if current page is lower than total # of pages, show links for next pages
if($currentpage < $totalpages)
{
	$next = intval($currentpage) + 1;
	$pages .= " <a href=\"memberlist.php?page=$next\">$next</a> ";
	if($currentpage < ($totalpages - 1)) // if current page is at least 2 less than total # of pages, show link for 2 pages after current page
	{
		$next2 = $next + 1;
		$pages .= " <a href=\"memberlist.php?page=$next2\">$next2</a> ";
	}
	$pages .= " <a href=\"memberlist.php?page=$next\">&gt;</a> "; //link > for next page
	$pages .= " <a href=\"memberlist.php?page=$totalpages\">&gt;&gt;</a> "; //link >> for last page
}

eval("\$memberlist .= \"".$templates->get("memberlist")."\";");
$templates->output($memberlist);
?>